package com.wrupple.muba.catalogs.client.activity.process.task.impl;

import java.util.Collections;
import java.util.List;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ProvidesKey;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;
import com.wrupple.muba.catalogs.client.activity.process.task.SelectionState;
import com.wrupple.muba.catalogs.client.events.DataSelectionEvent;
import com.wrupple.muba.catalogs.client.module.services.logic.impl.CatalogEntryKeyProviderImpl;
import com.wrupple.muba.catalogs.client.module.services.presentation.FilterableDataProvider;
import com.wrupple.muba.catalogs.client.view.BrowseView;
import com.wrupple.muba.catalogs.client.view.DataView;
import com.wrupple.muba.catalogs.domain.CatalogProcessDescriptor;
import com.wrupple.muba.common.client.application.DataCallback;
import com.wrupple.muba.common.shared.StateTransition;

public class SimpleSelectionState<T> implements SelectionState<T> {

	private class SelectionHandler implements
			com.google.gwt.view.client.SelectionChangeEvent.Handler {

		public SingleSelectionModel<T> selectionModel;

		public SelectionHandler(SingleSelectionModel<T> selectionModel) {
			super();
			this.selectionModel = selectionModel;
		}

		@Override
		public void onSelectionChange(SelectionChangeEvent event) {
			T selected = selectionModel.getSelectedObject();
			onSelectionChanged(selected);
		}
	}

	private class FireEvents extends DataCallback<List<T>> {

		private String catalogid;

		public FireEvents(String catalogid) {
			this.catalogid = catalogid;
		}

		@Override
		public void execute() {
			bus.fireEvent(new DataSelectionEvent(result, catalogid));
		}

	}
	
	

	protected DataView<T> view;
	protected Context context;
	protected FilterableDataProvider<T> dataProvider;
	protected EventBus bus;
	private StateTransition<List<T>> callback;

	public SimpleSelectionState(DataView<T> view,
			FilterableDataProvider<T> dataProvider) {
		super();
		this.view = view;
		this.dataProvider = dataProvider;
		
		SingleSelectionModel<T> selectionModel = new SingleSelectionModel<T>();
		view.getDisplay().setSelectionModel(selectionModel);

		selectionModel.addSelectionChangeHandler(new SelectionHandler(
				selectionModel));
	}


	@Override
	public Widget asWidget() {
		return view.asWidget();
	}

	@Override
	public void setContext(com.wrupple.muba.common.shared.State.Context context) {
		this.context = context;
	}

	@Override
	public void start(CatalogProcessDescriptor parameter,
			StateTransition<List<T>> onDone, EventBus bus) {
		this.bus = bus;
		this.callback = onDone;
		String catalogid = parameter.getSelectedType();

		ProvidesKey keyProvider = new CatalogEntryKeyProviderImpl();
		dataProvider.setCatalog(catalogid);
		
		//TODO force display update on change events
		onDone.hook(new FireEvents(catalogid));
	}

	@Override
	public void startSelection(StateTransition<List<T>> callback) {
		if(this.view instanceof BrowseView){
			((BrowseView) this.view).startSelection(Collections.emptyList(), callback);
		}
	}

	@Override
	public DataView<T> getBrowseView() {
		return view;
	}
	
	protected void onSelectionChanged(T selected) {
		List<T> singletonList = Collections.singletonList(selected);
		callback.setResultAndFinish(singletonList);
	}

}
